# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base

  # Use a layout on normal HTTP requests. Use no layout on AJAX requests.
  layout lambda { |controller| controller.request.xhr? ? "xhr" : "fragment" }
  # Include all helpers.
  helper :all
  # See ActionController::RequestForgeryProtection for details.
  protect_from_forgery

  # Scrub sensitive parameters from your log.
  filter_parameter_logging :password

  private

  before_filter :set_current_user, :require_login
  attr_accessor :current_user
  helper_method :current_user

  def logged_in?
    session[:user_id]
  end
  helper_method :logged_in?

  def require_login
    redirect_to login_url unless logged_in?
  end

  def set_current_user
    @current_user ||= User.find(session[:user_id]) if logged_in?
  end
end
